package com.ws.shop.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.stereotype.Repository;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.PointclogDao;
import com.ws.shop.entity.PointcLog;
import com.ws.shop.util.JdbcUtil;

@Repository(value = "pointclogDao")
public class PointclogDaoImpl extends BaseDaoImpl implements PointclogDao {

	private static Logger log = Logger.getLogger(PointclogDaoImpl.class);

	@SuppressWarnings("unchecked")
	public void displayUserPointc(final Page page, final long uid) {
		this.execute(new ConnectionCallback() {
			public Object doInConnection(Connection connect) {
				ResultSet rs = null;
				try {
					Statement stmt = null;
					try {
						StringBuilder sb = new StringBuilder(
								"SELECT COUNT(1) FROM jvf_pointclog where 1=1 ");
						StringBuilder query = new StringBuilder();
						query.append(" and uid=").append(uid);

						stmt = connect.createStatement();
						rs = stmt.executeQuery(sb.append(query).toString());
						// System.out.println(sb.toString());
						long totalnum = 0;
						if (rs.next()) {
							totalnum = rs.getLong(1);
							page.setTotalnum(totalnum);
						}
						JdbcUtil.closeResultSet(rs);

						if (totalnum > 0) {
							sb = new StringBuilder(
									"SELECT id, uid,NAME,payment,money,ADDTIME,remark  FROM  jvf_pointclog WHERE 1=1");
							rs = stmt.executeQuery(sb.append(query).append(" and uid=")
									.append(uid).append(" order by addtime desc limit ")
									.append(page.getStartrownum()).append(",").append(page.getPagesize()).toString());
							// System.out.println(sb.toString());
							PointcLog pointlog = null;
							while (rs.next()) {
								pointlog = new PointcLog();
								pointlog.setId(rs.getLong(1));
								pointlog.setUid(rs.getInt(2));
								pointlog.setName(rs.getString(3));
								pointlog.setPayment(rs.getString(4));
								pointlog.setMoney(rs.getInt(5));
								pointlog.setAddtime(rs.getTimestamp(6));
								pointlog.setRemark(rs.getString(7));
								page.addData(pointlog);
							}
						}
					} catch (Exception e) {
						log.error(e.getMessage(), e);
					} finally {
						JdbcUtil.close(rs, stmt, null);
					}
					return null;

				} catch (Exception e) {
					e.printStackTrace();
					log.error(" displayUserPointc!", e);
					return null;
				}
			}
		});
	}

	final String findPointclogListByPage_count = "select count(1) from jvf_pointclog";
	final String findPointclogListByPage_list = "select id,uid,name,payment,money,addtime,remark from jvf_pointclog";
	final String findPointclogListByPage_order = " order by id desc";

	public Page findPointclogListByPage(int pageno, int pagesize,
			String starttime, String endtime, String name) {
		StringBuilder query = new StringBuilder(" where 1=1");
		if (name != null) {
			query.append(" and name='").append(name).append("'");
		}
		query.append(" and addtime<='").append(endtime).append(" 23:59:59'")
			.append(" and addtime>='").append(starttime).append(" 00:00:00'");
		return this.findPage(pageno, pagesize, new StringBuilder(findPointclogListByPage_count).append(query).toString(),
				new StringBuilder(findPointclogListByPage_list).append(query).append(findPointclogListByPage_order));
	}

}
